package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.business.domain.FileLoanApplication;
import com.ruoyi.business.domain.vo.FileLoanApplicationVo;
import com.ruoyi.business.mapper.FileLoanApplicationMapper;
import com.ruoyi.business.service.IFileLoanApplicationService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateExtUtils;
import com.ruoyi.common.utils.StringExtUtils;
import com.ruoyi.workflow.service.IProcessInstanceService;
import com.ruoyi.workflow.utils.WorkFlowUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 借阅申请Service业务层处理
 *
 * @author sal
 * @date 2024-11-11
 */
@Service
public class FileLoanApplicationServiceImpl implements IFileLoanApplicationService {
    @Autowired
    private FileLoanApplicationMapper fileLoanApplicationMapper;
    @Autowired
    private IProcessInstanceService iProcessInstanceService;
    /**
     * 查询借阅申请
     *
     * @param id 借阅申请ID
     * @return 借阅申请
     */
    @Override
    public FileLoanApplication selectFileLoanApplicationById(Long id) {
        FileLoanApplication fileLoanApplication = fileLoanApplicationMapper.selectFileLoanApplicationById(id);
        WorkFlowUtils.setStatusFileValue(fileLoanApplication,fileLoanApplication.getId().toString());
        return fileLoanApplication;
    }

    /**
     * 查询借阅申请列表
     *
     * @param fileLoanApplication 借阅申请
     * @return 借阅申请
     */
    @Override
    public List<FileLoanApplication> selectFileLoanApplicationList(FileLoanApplication fileLoanApplication) {
        return fileLoanApplicationMapper.selectFileLoanApplicationList(fileLoanApplication);
    }

    @Override
    public TableDataInfo<FileLoanApplicationVo> selectPageFileLoanApplicationList(FileLoanApplicationVo fileIntranetExportVo, PageQuery pageQuery) {
        Page<FileLoanApplicationVo> result = fileLoanApplicationMapper.selectJoinPage(pageQuery.build(), FileLoanApplicationVo.class, this.buildQueryWrapper(fileIntranetExportVo));
        List<FileLoanApplicationVo> records = result.getRecords();
        if(CollectionUtil.isNotEmpty(records)){
            List<String> collectIds = records.stream().map(FileLoanApplicationVo::getId).collect(Collectors.toList());
            ArrayList<String> ids = new ArrayList<>();//因没有vo对象，只能转string
            WorkFlowUtils.setStatusListFileValue(records,collectIds,"id");
        }
        result.setRecords(records);
        return TableDataInfo.build(result);
    }

    private MPJLambdaWrapper<FileLoanApplication> buildQueryWrapper(FileLoanApplicationVo fileLoanApplication) {
        Map<String, Object> params = fileLoanApplication.getParams();
        MPJLambdaWrapper<FileLoanApplication> fileIntranetExportJoinLambdaWrapper = new MPJLambdaWrapper<FileLoanApplication>();
        fileIntranetExportJoinLambdaWrapper.selectAll(FileLoanApplication.class)
                .selectAs(SysUser::getNickName, FileLoanApplication::getBorrowerName)
                .selectAs(SysDept::getDeptName, FileLoanApplication::getBorrowingUnitName)
                .leftJoin(SysUser.class, SysUser::getUserId, FileLoanApplication::getBorrower)
                .leftJoin(SysDept.class, SysDept::getDeptId, FileLoanApplication::getBorrowingUnit)
                .eq(BeanUtil.isNotEmpty(fileLoanApplication.getBorrower()), FileLoanApplication::getBorrower, fileLoanApplication.getBorrower())
                .eq(BeanUtil.isNotEmpty(fileLoanApplication.getBorrowingUnit()), FileLoanApplication::getBorrowingUnit, fileLoanApplication.getBorrowingUnit())
                .like(BeanUtil.isNotEmpty(fileLoanApplication.getBorrowerFrom()), FileLoanApplication::getBorrowerFrom, fileLoanApplication.getBorrowerFrom())
                .eq(BeanUtil.isNotEmpty(fileLoanApplication.getFileType()), FileLoanApplication::getFileType, fileLoanApplication.getFileType())
                .eq(BeanUtil.isNotEmpty(fileLoanApplication.getWhetherPrint()), FileLoanApplication::getWhetherPrint, fileLoanApplication.getWhetherPrint())
                .eq(BeanUtil.isNotEmpty(fileLoanApplication.getStatus()), FileLoanApplication::getStatus, fileLoanApplication.getStatus())
                .eq(BeanUtil.isNotEmpty(fileLoanApplication.getApprovalStatus()), FileLoanApplication::getApprovalStatus, fileLoanApplication.getApprovalStatus());
        return fileIntranetExportJoinLambdaWrapper;
    }

    /**
     * 新增借阅申请
     *
     * @param fileLoanApplication 借阅申请
     * @return 结果
     */
    @Override
    public FileLoanApplication insertFileLoanApplication(FileLoanApplication fileLoanApplication) {
        fileLoanApplication.setCreateTime(DateExtUtils.getNowDate());
        fileLoanApplicationMapper.insert(fileLoanApplication);
        return fileLoanApplication;
    }

    /**
     * 修改借阅申请
     *
     * @param fileLoanApplication 借阅申请
     * @return 结果
     */
    @Override
    public FileLoanApplication updateFileLoanApplication(FileLoanApplication fileLoanApplication) {
        fileLoanApplication.setUpdateTime(DateExtUtils.getNowDate());
        fileLoanApplicationMapper.updateById(fileLoanApplication);
        return fileLoanApplication;
    }

    /**
     * 批量删除借阅申请
     *
     * @param ids 需要删除的借阅申请ID
     * @return 结果
     */
    @Override
    public int deleteFileLoanApplicationByIds(List<Long> ids) {
        for (Long id : ids) {
            String processInstanceId = iProcessInstanceService.getProcessInstanceId(id.toString());
            if (StringExtUtils.isNotBlank(processInstanceId)) { //删除相关流程数据
                iProcessInstanceService.deleteRuntimeProcessAndHisInst(processInstanceId);

            }
        }
        return fileLoanApplicationMapper.deleteBatchIds(ids);
    }

}
